package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper recordMapper;

    @Autowired
    private TbClueMapper clueMapper;


    @Override
    public void addTrackRecord(ClueTrackRecordVo tbClueTrackRecord, SysUser user) {

        TbClueTrackRecord record = new TbClueTrackRecord();

        // 数据填充，属性copy
        BeanUtils.copyBeanProp(record,tbClueTrackRecord);

        // 设置 类型为线索
        record.setType("0");

        // 设置 第一次跟进时间
        record.setCreateTime(new Date());

        // 设置跟进人
        record.setCreateBy(user.getUserName());

        // 数据填充
        // TODO1.添加跟进记录表
        recordMapper.insertClueRecord(record);


        // TODO2.修改 clue信息 表（修改状态为 已跟进）
        TbClue tbClue = new TbClue();

        // 设置 线索的状态 为已跟进
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());

        // 指明 该条记录的id
        tbClue.setId(record.getClueId());

        // 属性copy
        BeanUtils.copyBeanProp(tbClue,tbClueTrackRecord);
        clueMapper.updateTbClue(tbClue);


    }

    @Override
    public List<TbClueTrackRecord> getRecordList(Long clueId) {
       return recordMapper.list(clueId);
    }
}
